//
//  HRProgressHUD.m
//  haierapp
//
//  Created by zhang on 14-8-12.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRProgressHUD.h"

#define CONSTANT_IMAGEVIEW_NORMAL 102.0f
#define CONSTANT_IMAGEVIEW_LOADING 110.0f

@interface HRProgressHUD (){
    NSDictionary *_tipData;
}
@property (weak, nonatomic) IBOutlet UIImageView *hudImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewWidthCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeightCons;


@property (weak, nonatomic) IBOutlet UILabel *hudTitle;
@property (weak, nonatomic) IBOutlet UILabel *hudDesc;
@property (weak, nonatomic) IBOutlet UIButton *hudButton;
@end

@implementation HRProgressHUD


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self=[super initWithCoder:aDecoder];
    
    if (self) {
        //Initialization code
        
        _tipData=@{
                   @(HUDTYPE_ORDER_ALL):
                       @{@"imageName": @"none_all",@"title":@"抱歉，没有找到相关订单",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   @(HUDTYPE_ORDER_WAIT_SEND):
                       @{@"imageName": @"none_fa",@"title":@"抱歉，没有找到相关订单",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   @(HUDTYPE_ORDER_WAIT_RECEIVE):
                       @{@"imageName": @"none_shou",@"title":@"抱歉，没有找到相关订单",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   @(HUDTYPE_ORDER_WAIT_PAYMENT):
                       @{@"imageName": @"none_pay",@"title":@"抱歉，没有找到相关订单",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   @(HUDTYPE_ORDER_WAIT_COMMENT):
                       @{@"imageName": @"none_eva",@"title":@"抱歉，没有找到相关订单",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   @(HUDTYPE_ORDER_RETURN):
                       @{@"imageName": @"none_tui",@"title":@"抱歉，没有找到相关订单",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   
                   @(HUDTYPE_SHOPPING_CART):
                       @{@"imageName": @"none_cart",@"title":@"购物车内暂时没有商品",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   
                   @(HUDTYPE_COUPON):
                       @{@"imageName": @"none_quan",@"title":@"什么，还没有券？",@"desc":@"",@"button":@"马上去找"},
                   @(HUDTYPE_FAVORITE):
                       @{@"imageName": @"none_sc",@"title":@"抱歉，你还没有任何收藏",@"desc":@"快去挑选喜欢的商品吧",@"button":@"去逛逛"},
                   @(HUDTYPE_ADDRESS):
                       @{@"imageName": @"none_address",@"title":@"你还没有设置收货地址哟！",@"desc":@"快去设置收货地址吧！",@"button":@"新增地址"},
                   @(HUDTYPE_MESSAGE_CENTER):
                       @{@"imageName": @"none_message",@"title":@"抱歉，暂时没有新消息哦",@"desc":@"",@"button":@""},
                   @(HUDTYPE_SEARCH_HISTORY):
                       @{@"imageName": @"none_search_result",@"title":@"暂无搜索纪录",@"desc":@"",@"button":@""},
                   @(HUDTYPE_SEARCH):
                       @{@"imageName": @"none_search",@"title":@"抱歉，没有找到相关的商品",@"desc":@"",@"button":@""},
                   @(HUDTYPE_USER_SCORE):
                       @{@"imageName": @"none_jifen",@"title":@"什么，还没有积分?",@"desc":@"",@"button":@"马上去找"},
                   @(HUDTYPE_NO_WIFI):
                       @{@"imageName": @"none_net",@"title":@"数据加载失败",@"desc":@"请检查你的手机是否联网，点击按钮重新加载",@"button":@"重新加载"},
                   @(HUDTYPE_LOADING):
                       @{@"imageName": @"",@"title":@"",@"desc":@"",@"button":@""}
                   };
        
        
    }
    
    return  self;
}

#pragma mark - Class Methods
//类方法，构建对象
+(HRProgressHUD *)ProgressHUDWithType:(HUDTYPE)type{
    NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:NIBNAME_MESSAGE_HUD owner:self options:nil];
    HRProgressHUD *hud=[nibArray objectAtIndex:0];
    
    [hud setHUDType:type];
    return hud;
}

#pragma mark - Public Methods

//设置提示视图类型
-(void)setHUDType:(HUDTYPE)type{
    
    NSDictionary *tipItemData=[_tipData objectForKey:@(type)];
    
    if (type==HUDTYPE_LOADING) {
        NSMutableArray *imageArray=[NSMutableArray array];
        for (int i=1; i<=12; i++) {
            NSString *imageName=[NSString stringWithFormat:@"loading%d",i];
            UIImage *image=[UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
        
        _hudImageView.animationImages=imageArray;
        _hudImageView.animationDuration=1.0f;
        [_hudImageView startAnimating];
        
        _imageViewWidthCons.constant=CONSTANT_IMAGEVIEW_LOADING;
        _imageViewHeightCons.constant=CONSTANT_IMAGEVIEW_LOADING;
        
    }else{
        NSString *imageName=[tipItemData objectForKey:@"imageName"];
        _hudImageView.image=[UIImage imageNamed:imageName];
        
        _imageViewWidthCons.constant=CONSTANT_IMAGEVIEW_NORMAL;
        _imageViewHeightCons.constant=CONSTANT_IMAGEVIEW_NORMAL;
    }
    
    _hudTitle.text=[tipItemData objectForKey:@"title"];
    NSString *description=[tipItemData objectForKey:@"desc"];
    if ([description isEqualToString:@""] || description==nil) {
        //隐藏详细描述
        _hudDesc.hidden=YES;
    }else{
        _hudDesc.hidden=NO;
        _hudDesc.text=description;
    }
    
    NSString *buttonTitle=[tipItemData objectForKey:@"button"];
    
    if ([buttonTitle isEqualToString:@""] || buttonTitle==nil) {
        //隐藏按钮
        _hudButton.hidden=YES;
    }else{
        _hudButton.hidden=NO;
        [_hudButton setTitle:buttonTitle forState:UIControlStateNormal];
    }
    
    //按钮背景
    UIImage *bgImage=[[UIImage imageNamed:@"grey_border"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [_hudButton setBackgroundImage:bgImage forState:UIControlStateNormal];
}

//显示提示视图到指定视图上
-(void)showHUDInView:(UIView *)view withType:(HUDTYPE)type{
    [self setHUDType:type];
    
    [self showHUDInView:view];
}

//显示提示视图到指定视图上
-(void)showHUDInView:(UIView *)view{
    [view addSubview:self];
}

//隐藏提示视图到指定视图上
-(void)hideHUD{
    [_hudImageView stopAnimating];
    [self removeFromSuperview];
}


- (IBAction)onButtonTap:(id)sender {
    
    if ([_delegate respondsToSelector:@selector(tapHUDButtonAction:)]) {
        [_delegate tapHUDButtonAction:sender];
    }
}



@end
